Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
JavaScript TypeError: says a CSS declaration returns as "null"

Here is the code in question that seems to be causing issues:

        player.style.height = playerH + "px";
        function trackXY(){
        for(let i=0; i<pokeS.length; i++){
        let pokeS = document.getElementsByClassName('pikaStyle' + i);
        let xPlayer = parseInt(player.style.left + .5*player.style.width);
        let yPlayer = parseInt(player.style.top + .5*player.style.height);
        let xpokeS = parseInt(pokeS.style.left + .5*pokeS.style.width);
        let ypokeS = parseInt(pokeS.style.top + .5*pokeS.style.height);
        let rPlayer = .5*parseInt(player.style.height);
        let rpokeS = .5*parseInt(pokeS.style.height);
        let dX = xpokeS - xPlayer;
        let dY = ypokeS - yPlayer;
        let distance = Math.sqrt((dX*dX)+(dY*dY))
        if(distance <= (rpokeS+rPlayer)){
            alert("collison!" + pokeS.id);
        player.style.height = parseInt(player.style.height) + .5*parseInt(pokeS.style.height);
        }
      }
    }

it keeps either returning that "style" cannot be read or "left" is undefined. can someone advise? thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should use window.getComputedStyle() and getPropertyValue to get the style. You can't directly use element.style.height, it will return nothing.

The Window.getComputedStyle() method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain.

In your code, use:

window.getComputedStyle(player).getPropertyValue('height') 
window.getComputedStyle(pokeS).getPropertyValue('height')

Example:

let div = document.querySelector('div')
//return nothing
//console.log( div.style.height)
console.log(window.getComputedStyle(div).getPropertyValue('height'))
div{
height:50px;
}
<div></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda